package display {
	
	import display.screens.AboutScreen;
	import display.screens.ApartmentsScreen;
	import display.screens.BusinessScreen;
	import display.screens.GarageScreen;
	import display.screens.LayoutScreen;
	import display.screens.SearchScreen;
	
	import events.LoadingEvent;
	import events.ScreenEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import gs.TweenLite;
	
	import org.ilovecode.buttons.FloorButton;
	import org.ilovecode.buttons.NavigationButton;
	import org.ilovecode.events.GUIEvent;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.ScreenName;
	
	import utils.Navigation;
	import utils.method.capitalizeFirstLetter;
	
	/**
	 * Screen containing navigation and other screens container.
	 * This class acts like controller for different views.
	 * 
	 * @author Vjekoslav Ratkajec
	 */
	public class BasicScreen extends View {
		
		//{ region Stage elements
		
		//Container for screens
		public var screenContainer:MovieClip;
		
		/*
		//Buttons in navigation
		public var aboutButton:MovieClip;
		//Button to show info about apartments
		public var apartmentsButton:MovieClip;
		//Button to show info about business section
		public var businessButton:MovieClip;
		//Button to show garages
		public var garageButton:MovieClip;
		//Button to show search form
		public var searchButton:MovieClip;
		//Button to show frequently asked questions
		public var faqButton:MovieClip;
		//Button to show contact information
		public var contactButton:MovieClip;
		*/
		
		//Extra line which is not link
		public var graphicNavigation8:MovieClip;
		//Horizontal stripes acting as apartment floors
		public var graphicNavigation7:FloorButton;
		public var graphicNavigation6:FloorButton;
		public var graphicNavigation5:FloorButton;
		public var graphicNavigation4:FloorButton;
		public var graphicNavigation3:FloorButton;
		public var graphicNavigation2:FloorButton;
		//Horizontal stripes acting as business floors
		public var graphicNavigation1:FloorButton;
		public var graphicNavigation0:FloorButton;
		public var graphicNavigationMinus1:FloorButton;
		//Horizontal stripes acting as garage floors
		public var graphicNavigationMinus2:FloorButton;
		public var graphicNavigationMinus3:FloorButton;
		public var graphicNavigationMinus4:FloorButton;
		public var graphicNavigationMinus5:FloorButton;
		
		public var logoButton:SimpleButton;
		
		public var progressIcon:MovieClip;
		
		public var floorNumberTextField:TextField;
		
		//} endregion
		
		//{ region Variables
		
		//Controller for navigation behavior
		//private var navigation:Navigation = new Navigation();
		//Duration of tweening
		private var duration:Number = 0.3;
		
		//Color when menu is active
		private const ACTIVATED_COLOR:uint = 0xB7B7B7;
		//Color when menu is not active
		private const DEACTIVATED_COLOR:uint = 0x737172;
		//Color when mouse is over menu item
		private const SELECTED_COLOR:uint = 0xDC8816;
		
		private var currentScreenName:String = "";
		
		protected var _mainMenu:Array;
		
		protected var _graphicNavigation:Array;
		
		//} endregion
		
		//{ region Constructor and intialization
		
		/**
		 * Create new instance of Basic screen.
		 */
		public function BasicScreen() {
		}
		
		override protected function construct():void
		{
			super.construct();
			
			_mainMenu = new Array();
			_graphicNavigation = new Array();
			
			var name:String;
			for (var i:int = -5; i <= 7; i++) {
				name = ((i < 0) ? "graphicNavigationMinus" : "graphicNavigation") + Math.abs(i);
				FloorButton(this[name]).id = i;
				_graphicNavigation.push(this[name]);
			}
			
			initNavigationButton(ScreenName.getLabel(ScreenName.ABOUT).toUpperCase(), ScreenName.ABOUT, "aboutButton");
			initNavigationButton(ScreenName.getLabel(ScreenName.APARTMENTS).toUpperCase(), ScreenName.APARTMENTS, "apartmentsButton");
			initNavigationButton(ScreenName.getLabel(ScreenName.BUSINESS).toUpperCase(), ScreenName.BUSINESS, "businessButton");
			initNavigationButton(ScreenName.getLabel(ScreenName.GARAGE).toUpperCase(), ScreenName.GARAGE, "garageButton");
			initNavigationButton(ScreenName.getLabel(ScreenName.SEARCH).toUpperCase(), ScreenName.SEARCH, "searchButton");
			initNavigationButton(ScreenName.getLabel(ScreenName.FAQ).toUpperCase(), ScreenName.FAQ, "faqButton");
			initNavigationButton(ScreenName.getLabel(ScreenName.CONTACT).toUpperCase(), ScreenName.CONTACT, "contactButton");
		}
		
		private function initNavigationButton(label:String, type:String, name:String):void
		{
			var refMC:MovieClip = MovieClip(this.getChildByName(name + "Ref"));
			var nb:NavigationButton = new NavigationButton(label, type, name);
			nb.x = refMC.x;
			nb.y = refMC.y;
			this.addChild(nb);
			_mainMenu.push(nb);
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			for each (var graphicNavigation:Sprite in _graphicNavigation) 
			{
				graphicNavigation.addEventListener(MouseEvent.MOUSE_OVER, graphicNavigation_MouseOver);
				graphicNavigation.addEventListener(MouseEvent.MOUSE_OUT, graphicNavigation_MouseOut);
			}
			
			
			/*for (var i:int = -5; i <= 7; i++) {
				var name:String = ((i < 0) ? "graphicNavigationMinus" : "graphicNavigation") + Math.abs(i);
				if (i < -1) {
					FloorButton(this[name]).addEventListener(MouseEvent.CLICK, garageNavigationItemClicked);
				} else if (i >= -1 && i <= 1) {
					FloorButton(this[name]).addEventListener(MouseEvent.CLICK, businessNavigationItemClicked);
				} else {
					FloorButton(this[name]).addEventListener(MouseEvent.CLICK, layoutNavigationItemClicked);
				}
				FloorButton(this[name]).addEventListener(MouseEvent.MOUSE_OVER, graphicNavigation_MouseOver);
				FloorButton(this[name]).addEventListener(MouseEvent.MOUSE_OUT, graphicNavigation_MouseOut);
			}*/
			
			logoButton.addEventListener(MouseEvent.CLICK, logoButton_Click);
			
			model.addEventListener(GUIEvent.SWITCH_SCREEN, update);
			model.addEventListener(LoadingEvent.SHOW_PROGRESS_ICON, update);
			model.addEventListener(LoadingEvent.HIDE_PROGRESS_ICON, update);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			for each (var graphicNavigation:Sprite in _graphicNavigation) 
			{
				graphicNavigation.removeEventListener(MouseEvent.MOUSE_OVER, graphicNavigation_MouseOver);
				graphicNavigation.removeEventListener(MouseEvent.MOUSE_OUT, graphicNavigation_MouseOut);
			}
			
			/*for (var i:int = -5; i <= 7; i++) {
				var name:String = ((i < 0) ? "graphicNavigationMinus" : "graphicNavigation") + Math.abs(i);
				if (i < -1) {
					FloorButton(this[name]).removeEventListener(MouseEvent.CLICK, garageNavigationItemClicked);
				} else if (i >= -1 && i <= 1) {
					FloorButton(this[name]).removeEventListener(MouseEvent.CLICK, businessNavigationItemClicked);
				} else {
					FloorButton(this[name]).removeEventListener(MouseEvent.CLICK, layoutNavigationItemClicked);
				}
				FloorButton(this[name]).removeEventListener(MouseEvent.MOUSE_OVER, graphicNavigation_MouseOver);
				FloorButton(this[name]).removeEventListener(MouseEvent.MOUSE_OUT, graphicNavigation_MouseOut);
			}*/
			
			logoButton.removeEventListener(MouseEvent.CLICK, logoButton_Click);

			model.removeEventListener(GUIEvent.SWITCH_SCREEN, update);
			model.removeEventListener(LoadingEvent.SHOW_PROGRESS_ICON, update);
			model.removeEventListener(LoadingEvent.HIDE_PROGRESS_ICON, update);
		}
		
		override protected function update(event:Event):void
		{
			switch(event.type)
			{
				case GUIEvent.SWITCH_SCREEN:
					handle_SwitchScreen();
					break;
				case LoadingEvent.SHOW_PROGRESS_ICON:
					showProgress(true);
					break;
				case LoadingEvent.HIDE_PROGRESS_ICON:
					showProgress(false);
					break;
				default:
					super.update(event);
					break;
			}
		}
		
		protected function handle_SwitchScreen():void
		{
			//Remove all elements in content area
			reset();
			
			//Get screen and show it
			screenContainer.addChild(model.currentScreen);
			
			//TODO: Update navigation on event in GraphicFloorButton
			configGraphicNavigation();
		}
		
		
		/**
		 * Initialize screen.
		 * 
		 * @private
		 */
		/*private function init():void {
			initNavigation();
			initListeners();
			switchScreen("start");
		}*/
		
		/**
		 * Setup navigation behavior for menu items.
		 * 
		 * @private
		 */
		/*private function initNavigation():void {
			navigation.addSubscriber(aboutButton, true, navigationButtonClicked);
			navigation.addSubscriber(apartmentsButton, true, navigationButtonClicked);
			navigation.addSubscriber(businessButton, true, navigationButtonClicked);
			navigation.addSubscriber(garageButton, true, navigationButtonClicked);
			navigation.addSubscriber(searchButton, true, navigationButtonClicked);
			navigation.addSubscriber(faqButton, true, navigationButtonClicked);
			navigation.addSubscriber(contactButton, true, navigationButtonClicked);
		}*/
		
		/**
		 * Initialize listeners for components on screen.
		 * 
		 * @private
		 */
		/*private function initListeners():void {	
			//Set listeners for graphic navigation
			for (var i:int = -5; i <= 7; i++) {
				var name:String = ((i < 0) ? "graphicNavigationMinus" : "graphicNavigation") + Math.abs(i);
				if (i < -1) {
					MovieClip(this[name]).addEventListener(MouseEvent.CLICK, garageNavigationItemClicked);
				} else if (i >= -1 && i <= 1) {
					MovieClip(this[name]).addEventListener(MouseEvent.CLICK, businessNavigationItemClicked);
				} else {
					MovieClip(this[name]).addEventListener(MouseEvent.CLICK, layoutNavigationItemClicked);
				}
				MovieClip(this[name]).addEventListener(MouseEvent.MOUSE_OVER, graphicNavigation_MouseOver);
				MovieClip(this[name]).addEventListener(MouseEvent.MOUSE_OUT, graphicNavigation_MouseOut);
				MovieClip(this[name]).buttonMode = true;
			}
			
			logoButton.addEventListener(MouseEvent.CLICK, logoButton_Click);
		}*/
		
		//} endregion
		
		//{ region Handlers
		
		private function graphicNavigation_MouseOver(event:MouseEvent):void {
			var mc:FloorButton = FloorButton(event.currentTarget);
			TweenLite.to(mc, duration, { tint:SELECTED_COLOR } );
			
			var i:int = int(mc.name.slice(mc.name.length - 1));
			if (mc.name == "graphicNavigationMinus" + i) {
				floorNumberTextField.text = "-" + i;
				floorNumberTextField.y = mc.y - (floorNumberTextField.height / 2);
			} else {
				if (i != 0) { //Because 0 doesn't have text
					floorNumberTextField.text = "+" + i;
					floorNumberTextField.y = mc.y + mc.height - (floorNumberTextField.height / 2);
				}
			}
			
			floorNumberTextField.visible = true;
		}
		
		private function graphicNavigation_MouseOut(event:MouseEvent):void {
			var mc:FloorButton = FloorButton(event.currentTarget);
			var color:uint = getGraphicNavigationColor(mc.name);
			TweenLite.to(mc, duration, { tint:color } );
			
			floorNumberTextField.text = "";
			floorNumberTextField.visible = false;
		}
		
		private function search_SwitchScreen(event:ScreenEvent):void {
			event.currentTarget.removeEventListener(ScreenEvent.SWITCH_SCREEN, search_SwitchScreen);
			var layout:LayoutScreen = LayoutScreen(switchScreen("layout"));
			layout.addEventListener(LoadingEvent.SHOW_PROGRESS_ICON, layout_Show);
			layout.addEventListener(LoadingEvent.HIDE_PROGRESS_ICON, layout_Hide);
			layout.showApartment(event.result.apartmentID);
		}
		
		private function layout_Show(event:LoadingEvent):void {
			showProgress(true);
		}
		
		private function layout_Hide(event:LoadingEvent):void {
			showProgress(false);
		}
		
		/**
		 * Switch screen on target defined by clicked menu item.
		 * 
		 * @param	clickedButton	Menu button is clicked.
		 * @private
		 */
		private function navigationButtonClicked(clickedButton:MovieClip):void {
			var name:String = clickedButton.name.replace("Button", "");
			
			createScreen(name);
		}
		
		private function createScreen(name:String):void
		{
			switch (name) {
				case "about":
					var aboutScreen:AboutScreen = AboutScreen(switchScreen(name));
					aboutScreen.addEventListener(LoadingEvent.SHOW_PROGRESS_ICON, layout_Show);
					aboutScreen.addEventListener(LoadingEvent.HIDE_PROGRESS_ICON, layout_Hide);
					break;
				case "search":
					var searchScreen:SearchScreen = SearchScreen(switchScreen(name));
					searchScreen.addEventListener(ScreenEvent.SWITCH_SCREEN, search_SwitchScreen);
					break;
				case "apartments":
					var apartmentsScreen:ApartmentsScreen = ApartmentsScreen(switchScreen(name));
					apartmentsScreen.addEventListener(LoadingEvent.SHOW_PROGRESS_ICON, layout_Show);
					apartmentsScreen.addEventListener(LoadingEvent.HIDE_PROGRESS_ICON, layout_Hide);
					break;
				default:
					switchScreen(name);
			}
		}
		
		/**
		 * Horizontal stripe representing garage floor is clicked.
		 * 
		 * @param	event	Horizontal stripe is clicked.
		 * @private
		 */
		private function garageNavigationItemClicked(event:MouseEvent):void {
			var name:String = event.currentTarget.name;
			var floorNumber:int = int(name.replace("graphicNavigationMinus", ""));
			var garage:GarageScreen = GarageScreen(switchScreen("garage"));
			garage.showFloor(floorNumber);
		}
		
		/**
		 * Horizontal stripe representing apartment floor is clicked.
		 * 
		 * @param	event	Horizontal stripe is clicked.
		 * @private
		 */
		private function layoutNavigationItemClicked(event:MouseEvent):void {
			var layout:LayoutScreen = LayoutScreen(switchScreen("layout"));
			layout.addEventListener(LoadingEvent.SHOW_PROGRESS_ICON, layout_Show);
			layout.addEventListener(LoadingEvent.HIDE_PROGRESS_ICON, layout_Hide);
			var floorNumber:int = int(String(event.currentTarget.name).replace("graphicNavigation", ""));
			layout.showFloor(floorNumber);
		}
		
		/**
		 * Horizontal stripe representing business floor is clicked.
		 * 
		 * @param	event	Horizontal stripe is clicked.
		 * @private
		 */
		private function businessNavigationItemClicked(event:MouseEvent):void {
			var business:BusinessScreen = BusinessScreen(switchScreen("business"));
			
			var name = String(event.currentTarget.name);
			var floorNumber:int = int(name.slice(name.length - 1));
			if (name == "graphicNavigationMinus" + floorNumber) {
				floorNumber = -floorNumber;
			}
			
			business.showFloor(floorNumber);
		}
		
		/**
		 * Restart site with intro.
		 * 
		 * @param	event	Click on logo.
		 */
		private function logoButton_Click(event:MouseEvent):void {
			this.dispatchEvent(new ScreenEvent(ScreenEvent.SWITCH_SCREEN));
		}
		
		//} endregion
		
		//{ region Progress
		
		private function showProgress(visible:Boolean):void {
			progressIcon.visible = visible;
			logoButton.visible = !visible;
		}
		
		//} endregion
		
		//{ region Screen management
		
		/**
		 * Switch current screen for new one.
		 * 
		 * @param	name	Name of screen to open.
		 * @return	Opened screen.
		 * @private
		 */
		private function switchScreen(name:String):Sprite {
			//Remove all elements in content area
			reset();
			
			//Get screen and show it
			var screen:Sprite = Screens.getScreen(name);
			currentScreenName = name;
			screenContainer.addChild(screen);
			
			configGraphicNavigation();
			
			return screen;
		}
		
		private function configGraphicNavigation():void {
			for (var i:int = -5; i <= 8; i++) {
				var itemName:String = ((i < 0) ? "graphicNavigationMinus" : "graphicNavigation") + Math.abs(i);
				var color:uint = getGraphicNavigationColor(itemName);
				TweenLite.to(Sprite(this[itemName]), duration, { tint:color } );
			}
		}
		
		private function getGraphicNavigationColor(name:String):uint {
			var i:int = int(name.slice(name.length - 1));
			if (name == "graphicNavigationMinus" + i) {
				i = -i;
			}
			
			//Specially for 0 always return active because of navigation looks
			if (i == 0) return ACTIVATED_COLOR;
			
			var color:uint;
			if (currentScreenName != "garage" && currentScreenName != "business" && currentScreenName != "layout") {
				if (i < 0) {
					color = DEACTIVATED_COLOR;
				} else {
					color = ACTIVATED_COLOR;
				}
			} else {
				if (i < -1) {
					if (currentScreenName == "garage") {
						color = ACTIVATED_COLOR;
					} else {
						color = DEACTIVATED_COLOR;
					}
				} else if (i >= -1 && i <= 1) {
					if (currentScreenName == "business") {
						color = ACTIVATED_COLOR;
					} else {
						color = DEACTIVATED_COLOR;
					}
				} else {
					if (currentScreenName == "layout") {
						color = ACTIVATED_COLOR;
					} else {
						color = DEACTIVATED_COLOR;
					}
				}
			}
			
			return color;
		}
		
		/**
		 * Remove all elements in content container.
		 * 
		 * @private
		 */
		private function reset():void {
			while (screenContainer.numChildren > 0) {
				screenContainer.removeChildAt(0);
			}
			showProgress(false);
		}
		
		//} endregion
		
		
	}
	
}